<div class="sm-rightPanel">
    <div class="layui-anim layui-anim-upbit">
        <blockquote class="site-text layui-elem-quote">
            定时任务
        </blockquote>
        <div id="list_form" class="layui-form sm-operation">
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-normal" id="job_add">
                    <i class="iconfont icon-add"></i>新建
                </button>
                <!--<button class="layui-btn layui-btn-danger" id="job_del">-->
                <!--<i class="iconfont icon-delete"></i>删除-->
                <!--</button>-->
                <!--<button class="layui-btn layui-btn-warm" id="job_pause">-->
                <!--<i class="iconfont icon-zanting"></i>暂停-->
                <!--</button>-->
                <!--<button class="layui-btn layui-btn-warm" id="job_start">-->
                <!--<i class="iconfont icon-qiyong"></i>启动-->
                <!--</button>-->
            </div>
        </div>
        <table id="list_table" class="layui-hide" lay-filter="mainList"></table>
    </div>
</div>
<script>
    layui.use(['layer', 'table', 'form'], function () {
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var $ = layui.$;

        //表单重新渲染
        form.render();

        //表格渲染
        var tableObj = table.render({
            id: 'list_table'
            , elem: '#list_table'
            , url: '/sys/job/list'
            , align: "center"
            , cols: [[ //表头
                {type: 'checkbox'}
                , {type: 'numbers', title: '序号', width: 50}
                , {field: 'jobName', title: '任务名', width: 160, align: 'center'}
                , {field: 'jobGroup', title: '任务组', width: 150, align: 'center'}
                , {field: 'jobCron', title: '定时规则', width: 150, align: 'center'}
                , {
                    field: 'status', title: '状态', width: 120, align: 'center', templet: function (d) {
                        switch (d.status) {
                            case 'NORMAL':
                                return '<span class="font-success">正常</span>';
                            case 'PAUSED':
                                return '<span class="font-warm">暂停</span>';
                            case 'COMPLETE':
                                return '<span class="font-success">完成</span>';
                            case 'ERROR':
                                return '<span class="font-danger">异常</span>';
                            case 'BLOCKED':
                                return '<span class="font-danger">堵塞</span>';
                        }
                        return '<span class="font-success">正常</span>';
                        ;
                    }
                }
                , {field: 'jobClassName', title: '任务类', width: 150, align: 'center'}
                , {field: 'jobDesc', title: '描述', width: 190, align: 'center'}
                , {
                    field: 'lock', title: '操作', width: 280, templet: function (d) {
                        var html = '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="run">执行</button>'
                            + '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="edit">修改</button>'
                            + '<button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delete">删除</button>';
                        if (d.status == 'PAUSED') {
                            html += '<button class="layui-btn layui-btn-sm" lay-event="resume">恢复</button>';
                        } else {
                            html += '<button class="layui-btn layui-btn-sm" lay-event="pause">暂停</button>';
                            ;
                        }
                        return html;
                    }, unresize: true, align: 'center'
                }
            ]]
        });

        //监听工具条
        table.on('tool(mainList)', function (obj) {
            var data = obj.data
                , layEvent = obj.event;
            //修改
            if (layEvent == 'edit') {
                smartAui.dialog({
                    title: '修改'
                    , template: 'edit'
                    , saveUrl: '/sys/job/update'
                    , tableObj: tableObj
                    , htmlData: data
                })
            } else if (layEvent == "pause") {
                smartAui.confirm('确认要暂停吗？', function () {
                    smartAui.ajax({
                        url: '/sys/job/pause',
                        data: {
                            jobName: data.jobName,
                            jobGroup: data.jobGroup,
                        },
                        loading: true,
                        success: function (result) {
                            if (result.success) {
                                layer.msg("操作成功");
                            } else {
                                layer.msg("操作失败");
                            }
                            smartAui.reloadTable(tableObj);
                        },
                    })
                });
            } else if (layEvent == "resume") {
                smartAui.confirm('确认要启用吗？', function () {
                    smartAui.ajax({
                        url: '/sys/job/resume',
                        data: {
                            jobName: data.jobName,
                            jobGroup: data.jobGroup,
                        },
                        loading: true,
                        success: function (result) {
                            if (result.success) {
                                layer.msg("操作成功");
                            } else {
                                layer.msg("操作失败");
                            }
                            smartAui.reloadTable(tableObj);
                        },
                    })
                });
            } else if (layEvent == "run") {
                smartAui.confirm('确认执行吗？', function () {
                    smartAui.ajax({
                        url: '/sys/job/run',
                        data: {
                            jobName: data.jobName,
                            jobGroup: data.jobGroup,
                        },
                        loading: true,
                        success: function (result) {
                            if (result.success) {
                                layer.msg("操作成功");
                            } else {
                                layer.msg("操作失败");
                            }
                        },
                    })
                });
            } else if (layEvent == "delete") {
                smartAui.confirm('确认要删除吗？', function () {
                    smartAui.ajax({
                        url: '/sys/job/delete',
                        data: {
                            jobName: data.jobName,
                            jobGroup: data.jobGroup,
                        },
                        loading: true,
                        success: function (result) {
                            if (result.success) {
                                layer.msg("操作成功");
                            } else {
                                layer.msg("操作失败");
                            }
                            smartAui.reloadTable(tableObj);
                        },
                    })
                }, 0);
            }
        });

        // 添加
        $("#job_add").click(function () {
            smartAui.dialog({
                title: '添加'
                , template: 'add'
                , saveUrl: '/sys/job/save'
                , tableObj: tableObj
            })
        });
        // 重置密码
        $("#job_reset").click(function () {
            var data = table.checkStatus('list_table').data;//已选中数据
            if (data.length == 0) {
                layer.msg("请至少选择一条");
                return;
            }
            var arr = [];
            for (var i = 0; i < data.length; i++) {
                arr.push(data[i].userId)
            }
            smartAui.confirm('确认要重置密码吗？', function () {
                $.ajax({
                    type: "GET",
                    url: '/sys/user/resetpw',
                    data: {"userId": arr.join(",")},
                    success: function (result) {
                        if (result.success) {
                            layer.msg("重置成功");
                            smartAui.reloadTable(tableObj);
                        }
                    },
                    error: function () {
                        layer.msg("处理异常");
                    }
                })
            }, 0);
        });
        // 删除
        $("#job_del").click(function () {
            var data = table.checkStatus('list_table').data;//已选中数据
            if (data.length == 0) {
                layer.msg("请至少选择一条");
                return;
            }
            var arr = [];
            for (var i = 0; i < data.length; i++) {
                arr.push(data[i].userId);
            }
            smartAui.confirm("确认删除？", function () {
                $.ajax({
                    type: "GET",
                    url: '/sys/user/delete',
                    data: {"userId": arr.join(",")},
                    success: function (result) {
                        if (result.success) {
                            layer.msg("删除成功");
                            smartAui.reloadTable(tableObj);
                        }
                    },
                    error: function () {
                        layer.msg("删除异常");
                    }
                })
            }, 0);
        });
//        $("#query").click(function () {
//            smartAui.reloadTable(tableObj);
//        });
    });
</script>